package org.zkdn.modules.wlpt.basicinfo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsideDeviceMec;
import org.zkdn.modules.wlpt.basicinfo.entity.TRoad;
import org.zkdn.modules.wlpt.basicinfo.service.ITRoadService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.zkdn.modules.wlpt.basicinfo.vo.RoadVO;

/**
 * @Description: t_road
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Api(tags="t_road")
@RestController
@RequestMapping("/basicinfo/roadSurface/road")
@Slf4j
public class TRoadController extends JeecgController<TRoad, ITRoadService> {
	@Autowired
	private ITRoadService roadService;

	 @RequestMapping(value = "/listAll", method = RequestMethod.GET)
	 public Result<List<TRoad>> listAll(@RequestParam(name="roadName", required=false) String crossName) {
		 Result<List<TRoad>> result = new Result<>();
		 QueryWrapper<TRoad> queryWrapper = new QueryWrapper<>();
		 //查询未删除状态
		 queryWrapper.eq("del_flag", 0);
		 Optional.ofNullable(crossName).ifPresent(name -> queryWrapper.like("roadName", name));
		 List<TRoad> list = roadService.list(queryWrapper);
		 if(list==null||list.size()<=0) {
			 result.error500("未找到路口信息");
		 }else {
			 result.setResult(list);
			 result.setSuccess(true);
		 }
		 return result;
	 }

	/**
	 * 分页列表查询
	 *
	 * @param road
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "t_road-分页列表查询")
	@ApiOperation(value="t_road-分页列表查询", notes="t_road-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RoadVO>> queryPageList(TRoad road,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		//查询未删除状态
		queryWrapper.eq("roadAlias.del_flag", 0);

		Optional.ofNullable(road.getCode()).ifPresent(deviceId -> {
			queryWrapper.like("roadAlias.code", deviceId);
		});
		Optional.ofNullable(road.getRoadName()).ifPresent(serialNumber -> {
			queryWrapper.like("roadAlias.road_name", serialNumber);
		});
		Optional.ofNullable(road.getRoadAttr()).ifPresent(deviceType -> {
			queryWrapper.eq("roadAlias.road_attr", deviceType);
		});
		Optional.ofNullable(road.getRoadType()).ifPresent(manufacturerId -> {
			queryWrapper.eq("roadAlias.road_type", manufacturerId);
		});
		Optional.ofNullable(road.getZoneId()).ifPresent(poleId -> {
			queryWrapper.eq("roadAlias.zone_id", poleId);
		});
		Optional.ofNullable(road.getLevel()).ifPresent(crossId -> {
			queryWrapper.eq("roadAlias.level", crossId);
		});
		Optional.ofNullable(road.getRoadTag()).ifPresent(roadTag -> {
			List<String> crossTaglist = Arrays.stream(roadTag.split(","))
					.filter(s -> !s.isEmpty()) // 过滤空字符串
					.collect(Collectors.toList());
			queryWrapper.in("refTagAlias.dict_item_value", crossTaglist);
		});
		Page<RoadVO> page = new Page<>(pageNo, pageSize);
		IPage<RoadVO> pageList = roadService.queryPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param tRoad
	 * @return
	 */
	@AutoLog(value = "t_road-添加")
	@ApiOperation(value="t_road-添加", notes="t_road-添加")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_road:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TRoad tRoad) {
		return roadService.saveByValidate(tRoad);
	}

	/**
	 *  编辑
	 *
	 * @param tRoad
	 * @return
	 */
	@AutoLog(value = "t_road-编辑")
	@ApiOperation(value="t_road-编辑", notes="t_road-编辑")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_road:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TRoad tRoad) {
		return roadService.updateByValidate(tRoad);
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_road-通过id删除")
	@ApiOperation(value="t_road-通过id删除", notes="t_road-通过id删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_road:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		return roadService.deleteById(id);
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_road-批量删除")
	@ApiOperation(value="t_road-批量删除", notes="t_road-批量删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_road:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		return roadService.deleteByIds(ids);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "t_road-通过id查询")
	@ApiOperation(value="t_road-通过id查询", notes="t_road-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TRoad> queryById(@RequestParam(name="id",required=true) String id) {
		TRoad tRoad = roadService.getById(id);
		if(tRoad==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tRoad);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tRoad
    */
    //@RequiresPermissions("org.zkdn.modules.wlpt:t_road:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TRoad tRoad) {
        return super.exportXls(request, tRoad, TRoad.class, "t_road");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("t_road:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TRoad.class);
    }

}
